package org.sang.controller.system;

import com.mysql.jdbc.StringUtils;
import org.sang.bean.Employee;
import org.sang.bean.Role;
import org.sang.bean.User;
import org.sang.bean.RespBean;
import org.sang.common.UserUtils;
import org.sang.service.RoleService;
import org.sang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daigb on 2018/02/01.
 */
@RestController
@RequestMapping("/sys/user")
public class SystemUserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Map<String, Object> getUserByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, @RequestParam(defaultValue = "") String keywords, String company,String beginDateScope) {
        Map<String, Object> map = new HashMap<>();
        List<User> userByPage = userService.getUserByPage(page, size, keywords, company,beginDateScope);
        Long count = userService.getCountByKeywords(keywords,company, beginDateScope);
        map.put("emps", userByPage);
        map.put("count", count);
        return map;
    }
    @RequestMapping("/id/{userId}")
    public User getUserById(@PathVariable Long userId) {
        return userService.getUserById(userId);
    }
    @RequestMapping(value="/info",method = RequestMethod.GET)
    public User getCurrentUser() {
        return UserUtils.getCurrentUser();
    }
    @RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
    public RespBean deleteUser(@PathVariable Long userId) {
        if (userService.deleteUser(userId) == 1) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateUser(User user) {
        if (userService.updateUser(user) == 1) {
            return new RespBean("success", "更新成功!");
        }
        return new RespBean("error", "更新失败!");
    }

    @RequestMapping(value = "/roles", method = RequestMethod.PUT)
    public RespBean updateUserRoles(Long userId, Long[] rids) {
        if (userService.updateUserRoles(userId, rids) == rids.length) {
            return new RespBean("success", "更新成功!");
        }
        return new RespBean("error", "更新失败!");
    }

    @RequestMapping("/{keywords}")
    public List<User> getUsersByKeywords(@PathVariable(required = false) String keywords) {
        List<User> users = userService.getUsersByKeywords(keywords);
        return users;
    }


    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public RespBean userReg(User user) {
        boolean isReg=true;
        if(StringUtils.isNullOrEmpty(user.getPassword())){
            isReg=false;
        }
        int i = userService.userReg(user);
        if (i == 1) {
            if(isReg){
                return new RespBean("success", "注册成功!");
            }else{
                return new RespBean("success", "新增成功!");
            }
        } else if (i == -1) {
            if(isReg) {
                return new RespBean("error", "用户名重复，注册失败!");
            }else{
                return new RespBean("error", "用户名重复，新增失败!");
            }
        }
        if(isReg) {
            return new RespBean("error", "注册失败!");
        }else{
            return new RespBean("error", "新增失败!");
        }
    }

}
